package com.cx.controller;

import com.cx.dto.UserDTO;
import com.cx.dto.UserPageDTO;
import com.cx.mapper.UserMapper;
import com.cx.pojo.Menu;
import com.cx.service.UserService;
import com.cx.util.PageResult;
import com.cx.util.ResultUtil;
import com.cx.vo.JobAndCountVO;
import com.cx.vo.PermissionsVO;
import com.cx.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@Slf4j
@RequiredArgsConstructor
@RequestMapping("/user")
@Api(value = "用户接口")
public class UserController {

    private final UserService userService;

    @PostMapping("/login")
    @ApiOperation(value = "登录接口")
    public ResultUtil<UserVO> login(@RequestBody UserDTO userDTO) {
        log.info("用户登录！{}", userDTO);
        return userService.login(userDTO);
    }

    @PostMapping("/register")
    @ApiOperation(value = "注册接口")
    public ResultUtil register(@RequestBody UserDTO userDTO) {
        log.info("用户注册：{}", userDTO);
        return userService.register(userDTO);
    }

    @GetMapping("/selectUserById/{id}")
    @ApiOperation(value = "根据id查询用户接口")
    public ResultUtil<UserVO> selectUserById(@PathVariable("id") long id) {
        log.info("根据id查询用户：{}", id);
        return userService.selectUserById(id);
    }

    @PutMapping("/updateById")
    @ApiOperation(value = "根据id修改用户信息")
    public ResultUtil updateById(@RequestBody UserDTO userDTO) {
        log.info("修改用户信息！{}", userDTO);
        return userService.updateById(userDTO);
    }

    @PostMapping("/addUser/{ids}")
    @ApiOperation(value = "添加用户")
    public ResultUtil addUser(@PathVariable("ids") String ids, @RequestBody UserDTO userDTO) {
        log.info("添加用户！ids:{},userDTO:{}", ids, userDTO);
        return userService.addUser(ids, userDTO);
    }

    @GetMapping("/menus")
    @ApiOperation(value = "权限信息")
    public ResultUtil<List<Menu>> getMenus() {
        log.info("获取权限信息");
        return userService.getMenus();
    }


    @GetMapping("/page")
    @ApiOperation(value = "分页查询")
    public ResultUtil<PageResult<UserVO>> getUserList(UserPageDTO userPageDTO) {
        log.info("分页查询！userPageDTO:{}", userPageDTO);
        return userService.getUserList(userPageDTO);
    }

    @DeleteMapping("/deleteUserById")
    @ApiOperation(value = "根据id删除用户")
    public ResultUtil deleteUserById(@RequestParam("id") Long id) {
        log.info("根据id删除用户！ id:{}", id);
        return userService.deleteUserById(id);
    }

    @GetMapping("/selectJobAndCount")
    @ApiOperation(value = "查询各个职位及人数")
    public ResultUtil<List<JobAndCountVO>> selectJobAndCount() {
        log.info("查询各个职位及人数");
        return userService.selectJobAndCount();
    }

    @GetMapping("/getAuth")
    @ApiOperation("查询身份信息")
    public ResultUtil<List<PermissionsVO>> getAuths() {
        log.info("查询身份信息");
        return userService.getAuths();
    }

    @PutMapping("/authorization/{id}/{authId}")
    @ApiOperation("赋予身份")
    public ResultUtil authorization(
            @PathVariable("id") Integer id,
            @PathVariable("authId") Integer authId) {
        log.info("赋予身份:{},{}",id,authId);
        return userService.authorization(id,authId);
    }

    @GetMapping("/selectManager")
    @ApiOperation("查询项目负责人")
    public ResultUtil<List<UserVO>> selectManager(){
        log.info("查询项目负责人");
        return userService.selectManager();
    }
}
